﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Modify : System.Web.UI.Page
{
    BLL.Students bllStudent = new BLL.Students();
    BLL.Classes bllClasses = new BLL.Classes();
    protected MODEL.Students studentModel = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strId = Request.QueryString["id"];
            int intId = -1;
            if (string.IsNullOrEmpty(strId) || !int.TryParse(strId, out intId))
            {
                Response.Redirect("Default.aspx?msg=error参数格式错误！");
                Response.End();
            }
            studentModel = bllStudent.GetStudentById(strId);
            if (studentModel != null)
            {
                ViewState["stuId"] = studentModel.SID;
            }
            else
            {
                Response.Redirect("Default.aspx?msg=error参数格式错误！");
                Response.End();
            }
        }
        else
        {
            MODEL.Students model = new MODEL.Students();
            model.SID = Convert.ToInt32(ViewState["stuId"]);
            model.SPwd = Request.Form["txtPwd"];
            model.SCnName = Request.Form["txtCnName"];
            model.SCID = Convert.ToInt32(Request.Form["selClasses"]);
            model.SGender = Convert.ToBoolean(Request.Form["gender"].Equals("1") ? true : false);
            model.STel = Request.Form["txtTel"];
            bool res = bllStudent.ModifyStudent(model);
            Response.Redirect("Default.aspx?msg=修改成功!");
        }
    }

    protected string GetGender(bool gender, bool isMale)
    { 

        return isMale? (gender?"checked":""):(gender?"":"checked");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected string GetClassesSEL(int classId)
    { 
        StringBuilder sb = new StringBuilder();

        List<MODEL.Classes> listClasses = bllClasses.GetAllClasses();
        if (listClasses != null)
        {
            foreach (MODEL.Classes cla in listClasses)
            {
                //sb.Append("<option value = '" + cla.CID + "'>" + cla.CName + "</option>");
                sb.Append("<option "+(classId == cla.CID?"selected":"")+ " value = '"+ cla.CID + "'>"+cla.CName+"</option>");
            }
        }
        else
        {
            sb.Append("<option value = -1>Invalid Data</option>");
        }
        return sb.ToString();
    }
}